/**
 * BookingAction.java
 */
package com.prs.crm.action.meeting;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.assets.AssetsRegister;
import com.prs.crm.domain.meeting.MeetStart;
import com.prs.crm.domain.meeting.MyBooking;
import com.prs.crm.service.meeting.MeetStartService;
import com.prs.crm.service.meeting.MyBookingService;

/**
 * @author ZhuDongMei
 * @E-mail:1223984339@qq.com
 * @qq:1223984339
 * @version 创建时间：2011-3-16 上午11:12:28 简单说明
 */

public class BookingAction extends BaseAction {
	private static final long serialVersionUID = 1L;
    private MyBooking myBooking;
    private PaginationSupport pagination;
    private MyBookingService myBookingService;
    private MeetStart meetStart;
    private MeetStartService meetStartService;
    private Integer[] bookingIds;
    private Integer[] meetBookingIds;
	public Integer[] getMeetBookingIds() {
		return meetBookingIds;
	}
	public void setMeetBookingIds(Integer[] meetBookingIds) {
		this.meetBookingIds = meetBookingIds;
	}
	public Integer[] getBookingIds() {
		return bookingIds;
	}
	public void setBookingIds(Integer[] bookingIds) {
		this.bookingIds = bookingIds;
	}
	public MyBooking getMyBooking() {
		return myBooking;
	}
	public void setMyBooking(MyBooking myBooking) {
		this.myBooking = myBooking;
	}
	public MyBookingService getMyBookingService() {
		return myBookingService;
	}
	public void setMyBookingService(MyBookingService myBookingService) {
		this.myBookingService = myBookingService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public MeetStartService getMeetStartService() {
		return meetStartService;
	}
	public void setMeetStartService(MeetStartService meetStartService) {
		this.meetStartService = meetStartService;
	}
	public MeetStart getMeetStart() {
		return meetStart;
	}
	public void setMeetStart(MeetStart meetStart) {
		this.meetStart = meetStart;
	}
	@Action("listBooking")
	public String listBooking() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MyBooking.class);
		criteria.add(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetStartService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editBooking")
    public String editBooking(){
    	return SUCCESS;
    }
	@Action("editMeetBooking")
    public String editMeetBooking(){
    	return SUCCESS;
    }
	@Action("editBookings")
    public String editBookings(){
    	return SUCCESS;
    }
	@Action(value="updateBooking", results = {
			@Result(name = "success", type = "redirect", location = "editBookings?myBooking=${myBooking.getId()}"),
			@Result(name = "error", type = "chain", location = "listBooking") })
	public String updateBooking(){
		if(bookingIds!=null){
			this.myBooking = this.getMyBookingService().get(bookingIds[0]);
			if("结束".equals(myBooking.getState().trim())){
				this.addActionError("不能修改过去的预订会议！");
				return ERROR;
			}
		}
			return SUCCESS;
	}
	@Action("editMeetBookings")
    public String editMeetBookings(){
    	return SUCCESS;
    }
	@Action(value="updateMeetBooking", results = {
			@Result(name = "success", type = "redirect", location = "editMeetBookings?myBooking=${myBooking.getId()}"),
			@Result(name = "error", type = "chain", location = "listMeetBooking") })
	public String updateMeetBooking(){
		if(meetBookingIds!=null){
			this.myBooking = this.getMyBookingService().get(meetBookingIds[0]);
			if("结束".equals(myBooking.getState().trim())){
				this.addActionError("不能修改过去的预订会议！");
				return ERROR;
			}
		}
			return SUCCESS;
	}


	@Action(value = "saveBooking", results = {
			@Result(name = "success", type = "redirect", location = "listBooking"),
			@Result(name = "error", type = "chain", location = "editBooking") })
	public String saveBooking(){
		if(myBooking.getMeetRoom()==null){
			this.addActionError("会议室不能为空！");
			return ERROR;
		}
		if("".equals(myBooking.getName().trim())){
			this.addActionError("会议议题不能为空！");
			return ERROR;
		}
		if("".equals(myBooking.getStartTime())){
			this.addActionError("会议开始时间不能为空！");
			return ERROR;
		}
		if("".equals(myBooking.getEndTime())){
			this.addActionError("会议结束时间不能为空！");
			return ERROR;
		}
		myBooking.setBookingState("未预订");
		if(new Date().after(myBooking.getEndTime())){
			myBooking.setState("结束");
		}else{
			myBooking.setState("申请中");
		}
		this.getMyBookingService().save(myBooking);
		return SUCCESS;
	}
	@Action(value = "saveMeetBooking", results = {
			@Result(name = "success", type = "redirect", location = "listMeetBooking"),
			@Result(name = "error", type = "chain", location = "editMeetBooking") })
	public String saveMeetBooking(){
		if("".equals(myBooking.getName().trim())){
			this.addActionError("会议议题不能为空！");
			return ERROR;
		}else{
		myBooking.setBookingState("未预订");
		if(new Date().after(myBooking.getEndTime())){
			myBooking.setState("结束");
		}else{
			myBooking.setState("申请中");
		}
		this.getMyBookingService().save(myBooking);
		return SUCCESS;
		}
		
	}
	@Action(value = "cancelBooking", results = {
			@Result(name = "success", type = "redirect", location = "listBooking"),
			@Result(name = "error", type = "chain", location = "editBooking") })
	public String cancelBooking(){
		myBooking.setState("取消预订");
		this.getMyBookingService().save(myBooking);
		return SUCCESS;
	}
	@Action(value = "cancelMeetBooking", results = {
			@Result(name = "success", type = "redirect", location = "listMeetBooking"),
			@Result(name = "error", type = "chain", location = "editMeetBooking") })
	public String cancelMeetBooking(){
		myBooking.setState("取消预订");
		this.getMyBookingService().save(myBooking);
		return SUCCESS;
	}
	@Action(value = "recoverBooking", results = {
			@Result(name = "success", type = "redirect", location = "listBooking"),
			@Result(name = "error", type = "chain", location = "editBooking") })
	public String recoverBooking(){
		myBooking.setState("已确认");
		this.getMyBookingService().save(myBooking);
		return SUCCESS;
	}
	@Action(value = "recoverMeetBooking", results = {
			@Result(name = "success", type = "redirect", location = "listMeetBooking"),
			@Result(name = "error", type = "chain", location = "editMeetBooking") })
	public String recoverMeetBooking(){
		myBooking.setState("已确认");
		this.getMyBookingService().save(myBooking);
		return SUCCESS;
	}
	@Action("viewBooking")
	public String viewBooking(){
		return SUCCESS;
	}
	@Action(value = "deleteBooking", results = {
			@Result(name = "success", type = "redirect", location = "listBooking"),
			@Result(name = "error", type = "chain", location = "listBooking") })
	public String deleteBooking(){
		if(bookingIds!=null){
			this.getMyBookingService().batchRemove(bookingIds);
		}
		return SUCCESS;
	}
	@Action(value = "deleteMeetBooking", results = {
			@Result(name = "success", type = "redirect", location = "listMeetBooking"),
			@Result(name = "error", type = "chain", location = "listMeetBooking") })
	public String deleteMeetBooking(){
		if(meetBookingIds!=null){
			this.getMyBookingService().batchRemove(meetBookingIds);
		}
		return SUCCESS;
	}
	
	@Action(value = "comfirmBooking", results = {
			@Result(name = "error", type = "chain", location = "listMeetBooking") })
	public String comfirmBooking(){
		if(meetBookingIds!=null){
			this.myBooking=this.getMyBookingService().get(meetBookingIds[0]);
			return SUCCESS;
		}
		else{
			this.addActionError("请选择数据");
			return ERROR;
		}
		
	}
	
	@Action(value = "saveComfirmBooking", results = {
			@Result(name = "success", type = "redirect", location = "listMeetBooking"),
			@Result(name = "error", type = "chain", location = "comfirmBooking") })
	public String saveComfirmBooking(){
		this.getMyBookingService().save(myBooking);
		return SUCCESS;
	}
	
	
	@Action("listMeetBooking")
	public String listMeetBooking() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MyBooking.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetStartService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("selectMyBooking")
	public String selectMyBooking(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(MyBooking.class)
									.add(Restrictions.eq("bookingState", "未预订")).add(Restrictions.eq("state", "已确认"));
		criteria.add(Restrictions.like("people", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%"));
		this.setPagination(this.getMyBookingService().listByPage(criteria, 
				this.getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
}
